package com.wangp.mywrite.coding;

import java.util.*;

/**
 * @description: 1233. 删除子文件夹
 * @author tooStronger
 * @date 2023/2/8
 */
public class RemoveSubfolders {

    public List<String> removeSubfolders(String[] folder) {
        Arrays.sort(folder);
        Set<String> set = new HashSet<>();

        int count = 0;
        for (String f : folder) {
            for (String s : set) {
                if (f.startsWith(s) && f.substring(s.length()).charAt(0) == '/') {
                    break;
                }
                count++;
            }
            if (set.size() == 0 || count == set.size()) {
                set.add(f);
            }
            count = 0;
        }
        return new ArrayList<>(set);
    }

    public List<String> answer(String[] folder) {
        Arrays.sort(folder);
        List<String> ans = new ArrayList();
        ans.add(folder[0]);
        for (int i = 1; i < folder.length; i++) {
            int pre = ans.get(ans.size() - 1).length();
            if (pre > folder[i].length() || !ans.get(ans.size() - 1).equals(folder[i].substring(0, pre)) || folder[i].charAt(pre) != '/') {
                ans.add(folder[i]);
            }
        }
        return ans;
    }

    public static void main(String[] args) {
//        System.out.println(new RemoveSubfolders().removeSubfolders(new String[]{"/a", "/a/b", "/c/d", "/c/d/e", "/c/f"}));
        System.out.println(new RemoveSubfolders().removeSubfolders(new String[]{"/a/b/c", "/a/b/ca", "/a/b/d"}));
    }

}
